/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package zaizai.com.slice;

import com.ajguan.ResourceTable;
import com.ajguan.library.EasyRefreshLayout;
import com.ajguan.library.LoadModel;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.text.Font;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import zaizai.com.SimpleAdapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 仅加载界面
 */
public class OnlyLoadingAbilitySlice extends AbilitySlice {
    private EasyRefreshLayout easyRefreshLayout;
    private SimpleAdapter adapter;
    private EventHandler eventHandler;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter
                .getInstance(OnlyLoadingAbilitySlice.this)
                .parse(ResourceTable.Layout_ability_only_loading, null, false));
        initView();
        initListener();
        initData();
    }
    @Override
    protected void onStop() {
        easyRefreshLayout.release();
        easyRefreshLayout = null;
        eventHandler.removeAllEvent();
        eventHandler = null;
        super.onStop();
    }
    /**
     * 模拟数据
     */
    private void initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("EasyRefreshLayout index :" + i);
        }
        adapter.setNewData(list);

    }

    /**
     * 设置监听
     */
    private void initListener() {
        easyRefreshLayout.setEnablePullToRefresh(false);
        easyRefreshLayout.setLoadMoreModel(LoadModel.COMMON_MODEL);

        easyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {

                final List<String> list = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    list.add("this is  new load data >>>>" +  new Date().toLocaleString());
                }

               eventHandler.postTask(() -> {
                    easyRefreshLayout.closeLoadView();
                    adapter.getData().addAll(list);
                    adapter.notifyDataChanged();
                }, 500);


            }

            @Override
            public void onRefreshing() {

            }
        });
    }

    /**
     * 初始化view
     */
    private void initView() {
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        Button titleBar = (Button) findComponentById(ResourceTable.Id_title_bar);
        titleBar.setFont(Font.DEFAULT_BOLD);
        easyRefreshLayout = (EasyRefreshLayout) findComponentById(ResourceTable.Id_easylayout);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        adapter = new SimpleAdapter();
        listContainer.setItemProvider(adapter);
//        banner = (ConvenientBanner) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_convenientbanner_view, null, false);
    }
}
